﻿using Admin.DAL;
using Admin.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Admin.Controllers
{
    public class FloorAdminController : Controller
    {
        //
        // GET: /FloorAdmin/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult zxv()
        {
            return View();
        }
        public ActionResult FloorAdmin(string id)
        {
            FloorAdminDAL flo = new FloorAdminDAL();
            List<FloorAdminModel> list = flo.TANG(id);


            return View(list);
        }
        //delete
        public ActionResult deleteFloorAdmin(string id)
        {
            FloorAdminDAL dal = new FloorAdminDAL();
            List<FloorAdminModel> list = dal.deleteTANG(id);
            return View(list);
        }

        [HttpPost, ActionName("DeleteFloorAdmin")]
        [ValidateAntiForgeryToken]

        public ActionResult DeleteFloorAdmin(string id)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from TANG where MATANG='" + id + "'", conn);
            cmd.ExecuteNonQuery();
            return RedirectToAction("FloorAdmin");
        }

        [HttpGet]
        public ActionResult insertFloorAdmin(string id)
        {

            return View();
        }
        private bool isValidContentType(string contentType)
        {
            return contentType.Equals("image/png") || contentType.Equals("image/gif") ||
                contentType.Equals("image/jpg") || contentType.Equals("image/jpeg");
        }
        private bool isValidContentLength(int contentLength)
        {
            return ((contentLength / 1024) / 1024) < 1;
        }
        [HttpPost]
        public ActionResult insertFloorAdmin(FloorAdminModel model, FormCollection collect, IEnumerable<HttpPostedFileBase> photo,string[] a)
        {
              a = new string[15];
                int i=0;
                var asd = new FloorAdminModel();
                List<FloorAdminModel> list = new List<FloorAdminModel>();
                SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
                conn.Open();



                foreach (var file in photo)
                {
                    if (file!=null && !isValidContentType(file.ContentType))
                    {
                        ViewBag.Error = "Bạn chỉ được chọn file hình có kiểu jpg, png, gif & jpeg.";
                        return View(list);
                    }
                    else if (file != null && !isValidContentLength(file.ContentLength))
                    {
                        ViewBag.Error = "File hình vượt quá dung lượng quy định (<1M)";
                        return View(list);
                    }

                    else
                    {
                        if (file != null && file.ContentLength > 0)
                        {
                            var fileName = Path.GetFileName(file.FileName);
                            var path = Path.Combine(Server.MapPath("~/Content/images/upload"), fileName);
                            file.SaveAs(path);
                            a[i] = file.FileName;
                        }
                    }
                    i++;
                }

                if(model.TENTANG == null)
                {
                    return View();
                }

                if (a[0] == null)
                {
                    model.LISTIMG1 = "";
                }
                else
                {
                    model.LISTIMG1 = a[0];
                }
                if (a[1] == null)
                {
                    model.LISTIMG2 = "";
                }
                else
                {
                    model.LISTIMG2 = a[1];
                }
                if (a[2] == null)
                {
                    model.LISTIMG3 = "";
                }
                else
                {
                    model.LISTIMG3 = a[2];
                }
                if (a[3] == null)
                {
                    model.LISTIMG4 = "";
                }
                else
                {
                    model.LISTIMG4 = a[3];
                }
                if (a[4] == null)
                {
                    model.LISTIMG5 = "";
                }
                else
                {
                    model.LISTIMG5 = a[4];
                }
                if (a[5] == null)
                {
                    model.LISTIMG6= "";
                }
                else
                {
                    model.LISTIMG6 = a[5];
                }
                if (a[6] == null)
                {
                    model.LISTIMG7 = "";
                }
                else
                {
                    model.LISTIMG7 = a[6];
                }
                if (a[7] == null)
                {
                    model.LISTIMG8 = "";
                }
                else
                {
                    model.LISTIMG8 = a[7];
                }
                if (a[8] == null)
                {
                    model.LISTIMG9 = "";
                }
                else
                {
                    model.LISTIMG9 = a[8];
                }
                if (a[9] == null)
                {
                    model.LISTIMG10 = "";
                }
                else
                {
                    model.LISTIMG10 = a[9];
                }
                if (a[10] == null)
                {
                    model.LISTIMG11 = "";
                }
                else
                {
                    model.LISTIMG11 = a[10];
                }
                if (a[11] == null)
                {
                    model.LISTIMG12 = "";
                }
                else
                {
                    model.LISTIMG11 = a[12];
                }
                if (a[12] == null)
                {
                    model.LISTIMG13 = "";
                }
                else
                {
                    model.LISTIMG13 = a[12];
                }
                if (a[13] == null)
                {
                    model.LISTIMG14 = "";
                }
                else
                {
                    model.LISTIMG14 = a[13];
                }
                if (a[14] == null)
                {
                    model.LISTIMG15 = "";
                }
                else
                {
                    model.LISTIMG15 = a[14];
                }


                SqlCommand command = new SqlCommand("insert into TANG (TENTANG,LIST1,LIST2,LIST3,LIST4,LIST5,LIST6,LIST7,LIST8,LIST9,LIST10,LIST11,LISTIMG1,LISTIMG2,LISTIMG3,LISTIMG4,LISTIMG5,LISTIMG6,LISTIMG7,LISTIMG8,LISTIMG9,LISTIMG10,LISTIMG11,LISTIMG12,LISTIMG13,LISTIMG14,LISTIMG15) Values(N'" + model.TENTANG + "',N'" + model.LIST1 + "',N'" + model.LIST2 + "',N'" + model.LIST3 + "',N'" + model.LIST4 + "',N'" + model.LIST5 + "',N'" + model.LIST6 + "',N'" + model.LIST7 + "',N'" + model.LIST8 + "',N'" + model.LIST9 + "',N'" + model.LIST10 + "',N'" + model.LIST11 + "',N'" + model.LISTIMG1 + "',N'" + model.LISTIMG2 + "',N'" + model.LISTIMG3 + "',N'" + model.LISTIMG4 + "',N'" + model.LISTIMG5 + "',N'" + model.LISTIMG6 + "',N'" + model.LISTIMG7 + "',N'" + model.LISTIMG8 + "',N'" + model.LISTIMG9 + "',N'" + model.LISTIMG10 + "',N'" + model.LISTIMG11 + "',N'" + model.LISTIMG12 + "',N'" + model.LISTIMG13 + "',N'" + model.LISTIMG14 + "',N'" + model.LISTIMG15 + "')", conn);
                SqlDataReader dr = command.ExecuteReader();


                return RedirectToAction("FloorAdmin");

            
        }

        [HttpGet]
        public ActionResult editFloorAdmin(string id)
        {

            FloorAdminDAL flo = new FloorAdminDAL();
            List<FloorAdminModel> list = flo.editTANG(id);
            return View(list);



        }

        [HttpPost]
        public ActionResult editFloorAdmin(string id, FloorAdminModel model, FormCollection collect, IEnumerable<HttpPostedFileBase> photo, string[] a)
        {

            a = new string[15];
            int i = 0;
            FloorAdminDAL dal = new FloorAdminDAL();
            List<FloorAdminModel> list = dal.editTANG(id);
            SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
            conn.Open();



            foreach (var file in photo)
            {
                if (file != null && !isValidContentType(file.ContentType))
                {
                    ViewBag.Error = "Bạn chỉ được chọn file hình có kiểu jpg, png, gif & jpeg.";
                    return View(list);
                }
                else if (file != null && !isValidContentLength(file.ContentLength))
                {
                    ViewBag.Error = "File hình vượt quá dung lượng quy định (<1M)";
                    return View(list);
                }

                else
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/Content/images/upload"), fileName);
                        file.SaveAs(path);
                        a[i] = file.FileName;
                    }
                }
                i++;
            }

            model.TENTANG = collect["tentang"];
            model.LIST1 = collect["nhomsp1"];
            model.LIST2 = collect["nhomsp2"];
            model.LIST3 = collect["nhomsp3"];
            model.LIST4 = collect["nhomsp4"];
            model.LIST5 = collect["nhomsp5"];
            model.LIST6 = collect["nhomsp6"];
            model.LIST7 = collect["nhomsp7"];
            model.LIST8 = collect["nhomsp8"];
            model.LIST9 = collect["nhomsp9"];
            model.LIST10 = collect["nhomsp10"];
            model.LIST11 = collect["nhomsp11"];

            //model.LISTIMG1 = a[0];
            //model.LISTIMG2 = a[1];
            //model.LISTIMG3 = a[2];
            //model.LISTIMG4 = a[3];
            //model.LISTIMG5 = a[4];
            //model.LISTIMG6 = a[5];
            //model.LISTIMG7 = a[6];
            //model.LISTIMG8 = a[7];
            //model.LISTIMG9 = a[8];
            //model.LISTIMG10 = a[9];
            //model.LISTIMG11 = a[10];
            //model.LISTIMG12 = a[11];
            //model.LISTIMG13 = a[12];
            //model.LISTIMG14 = a[13];
            //model.LISTIMG15 = a[14];



            if (a[0] == null)
            {
                model.LISTIMG1 = collect["photo1"];
            }
            else
            {
                model.LISTIMG1 = a[0];
            }
            if (a[1] == null)
            {
                model.LISTIMG2 = collect["photo2"];
            }
            else
            {
                model.LISTIMG2 = a[1];
            }
            if (a[2] == null)
            {
                model.LISTIMG3 = collect["photo3"];
            }
            else
            {
                model.LISTIMG3 = a[2];
            }
            if (a[3] == null)
            {
                model.LISTIMG4 = collect["photo4"];
            }
            else
            {
                model.LISTIMG4 = a[3];
            }
            if (a[4] == null)
            {
                model.LISTIMG5 = collect["photo5"];
            }
            else
            {
                model.LISTIMG5 = a[4];
            }
            if (a[5] == null)
            {
                model.LISTIMG6 = collect["photo6"];
            }
            else
            {
                model.LISTIMG6 = a[5];
            }
            if (a[6] == null)
            {
                model.LISTIMG7 = collect["photo7"];
            }
            else
            {
                model.LISTIMG7 = a[6];
            }
            if (a[7] == null)
            {
                model.LISTIMG8 = collect["photo8"];
            }
            else
            {
                model.LISTIMG8 = a[7];
            }
            if (a[8] == null)
            {
                model.LISTIMG9 = collect["photo9"];
            }
            else
            {
                model.LISTIMG9 = a[8];
            }
            if (a[9] == null)
            {
                model.LISTIMG10 = collect["photo10"];
            }
            else
            {
                model.LISTIMG10 = a[9];
            }
            if (a[10] == null)
            {
                model.LISTIMG11 = collect["photo11"];
            }
            else
            {
                model.LISTIMG11 = a[10];
            }
            if (a[11] == null)
            {
                model.LISTIMG12 = collect["photo12"];
            }
            else
            {
                model.LISTIMG11 = a[12];
            }
            if (a[12] == null)
            {
                model.LISTIMG13 = collect["photo13"];
            }
            else
            {
                model.LISTIMG13 = a[12];
            }
            if (a[13] == null)
            {
                model.LISTIMG14 = collect["photo14"];
            }
            else
            {
                model.LISTIMG14 = a[13];
            }
            if (a[14] == null)
            {
                model.LISTIMG15 = collect["photo15"];
            }
            else
            {
                model.LISTIMG15 = a[14];
            }


            SqlCommand command = new SqlCommand("Update TANG set TENTANG='" + model.TENTANG + "',LIST1=N'" + model.LIST1 + "', LIST2=N'" + model.LIST2 + "', LIST3=N'" + model.LIST3 + "', LIST4=N'" + model.LIST4
                + "', LIST5=N'" + model.LIST5 + "', LIST6=N'" + model.LIST6 + "',LIST7=N'" + model.LIST7 + "',LIST8=N'" + model.LIST8 + "',LIST9=N'" + model.LIST9 + "',LIST10=N'" + model.LIST10 + "',LIST11=N'" + model.LIST1
                + "',LISTIMG1=N'" + model.LISTIMG1 + "',LISTIMG2=N'" + model.LISTIMG2 + "',LISTIMG3=N'" + model.LISTIMG3 + "',LISTIMG4=N'" + model.LISTIMG4 + "',LISTIMG5=N'" + model.LISTIMG5 + "',LISTIMG6=N'" + model.LISTIMG6
                + "',LISTIMG7=N'" + model.LISTIMG7 + "',LISTIMG8=N'" + model.LISTIMG8 + "',LISTIMG9=N'" + model.LISTIMG9 + "',LISTIMG10=N'" + model.LISTIMG10 + "',LISTIMG11=N'" + model.LISTIMG11 + "',LISTIMG12=N'" + model.LISTIMG12
                + "',LISTIMG13=N'" + model.LISTIMG13 + "',LISTIMG14=N'" + model.LISTIMG14 + "',LISTIMG15=N'" + model.LISTIMG15 + "' where MATANG='" + id + "'", conn);
            SqlDataReader dr = command.ExecuteReader();


            return RedirectToAction("FloorAdmin");

        }

    }

}